package la.iok.hzsvn.lewin.movie.service;

import la.iok.hzsvn.lewin.movie.config.DfsProperties;
import la.iok.hzsvn.lewin.movie.core.Token;
import la.iok.hzsvn.lewin.movie.core.exception.ErrorCode;
import la.iok.hzsvn.lewin.movie.entity.*;
import la.iok.hzsvn.lewin.movie.mapper.MovieAssistFileMapper;
import la.iok.hzsvn.share.annotations.NotNull;
import la.iok.hzsvn.share.annotations.Nullable;
import la.iok.hzsvn.share.dfs.model.DfsDescriptor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;

@Service
public class MovieAssistFileServiceImpl extends MovieFileServiceImpl<MovieAssistFile, MovieAssistFileMapper> implements MovieAssistFileService{
    private final MovieService movieService;
    public MovieAssistFileServiceImpl(MovieAssistFileMapper mapper,
                                      WebDfsService webDfsService,
                                      DfsProperties dfsProperties,
                                      MovieService movieService) {
        super(mapper, webDfsService, dfsProperties);
        this.movieService = movieService;
    }

    @Transactional
    @Override
    public void uploadAssist(@NotNull Token token, @Nullable Long movieId, @NotNull MultipartFile file, @NotNull String path) {
        Movie movie = movieService.validate(token,movieId);
        Long id = uploadFile(file);
        decrement(movie.getAssistFileId());
        movieService.update(movieId,"assistFileId",id);
        movieService.update(movieId,"assist",path);
        increment(id);
    }

    @Transactional
    @Override
    public void clearAssist(Token token, Long movieId) {
        Movie movie = movieService.validate(token,movieId);
        decrement(movie.getAssistFileId());
        movieService.update(movieId,"assistFileId",0L);
        movieService.update(movieId,"assist","");
    }

    @Override
    public void downloadFile(@NotNull Token token, @NotNull HttpServletResponse response, @NotNull Long movieId) {
        Movie movie = movieService.validate(token,movieId);
        downloadFile(response,movie.getAssistFileId(),movie.getName());
    }

    @Override
    public void downloadFile(@NotNull HttpServletResponse response, @NotNull Long id,@NotNull String movieName) {
        MovieAssistFile file = select(id);
        ErrorCode.MOVIE_ASSIST_NOT_EXIST.assertNotNull(file,movieName);
        DfsDescriptor descriptor = webDfsService.dfsFile(file.getPath());
        ErrorCode.MOVIE_ASSIST_FILE_NOT_FOUND.assertTrue(webDfsService.exist(descriptor),movieName,descriptor.path());
        webDfsService.downloadFile(response,descriptor, file.getFileName());
    }

    @Override
    protected void validateReference(@NotNull MovieAssistFile file) {
        MovieInstance item = executeSelect(MovieInstance.class,"select * from "
                + MovieInstance.class.getSimpleName() + " where fileDeleted=0 and (assistFileId=" + file.getId() + " or assistHtmlFileId=" + file.getId() + ") limit 1");
        ErrorCode.FILE_REFERENCED_BY_OTHER.assertNull(item,file.getPath(),MovieInstance.class.getSimpleName());

        Movie item2 = executeSelect(Movie.class,"select * from "
                + Movie.class.getSimpleName() + " where assistFileId=" + file.getId() + " limit 1");
        ErrorCode.FILE_REFERENCED_BY_OTHER.assertNull(item2,file.getPath(),Movie.class.getSimpleName());

        MovieAssistImage item3 = executeSelect(MovieAssistImage.class,"select * from "
                + MovieAssistImage.class.getSimpleName() + " where fileDeleted=0 and fileId=" + file.getId() + " limit 1");
        ErrorCode.FILE_REFERENCED_BY_OTHER.assertNull(item3,file.getPath(),MovieAssistImage.class.getSimpleName());
    }
}
